﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using UFC.Common;

namespace MESGateWay.WinSMESContainer
{
    partial class Send_MESContainer : ServiceBase
    {
        public Send_MESContainer()
        {
            InitializeComponent();
        }

        private System.Timers.Timer m_SyncTimer;
        private System.Timers.Timer m_SendToErpWorkflowTimer;
        private System.Timers.Timer m_ErpMfgorderPriorityToMes;
        private System.Timers.Timer m_SendBomDiffToERPTimer;//add zhunan 2025.9.25
        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            //int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());
            //m_SyncTimer = new System.Timers.Timer();
            //SyncTimerStatr(ref m_SyncTimer, m_SyncTimer_Elapsed, intSync * 2 * 60 * 1000);//发送批次
            //m_SendToErpWorkflowTimer = new System.Timers.Timer();
            //SyncTimerStatr(ref m_SendToErpWorkflowTimer, m_SendToErpWorkflowTimer_Elapsed, intSync * 60 * 1000);//发送工艺信息给erp
            //m_ErpMfgorderPriorityToMes = new System.Timers.Timer();//add by longb 2023-11-21
            //SyncTimerStatr(ref m_ErpMfgorderPriorityToMes, m_ErpMfgorderPriorityToMes_Elapsed, intSync * 60 * 1000);//Erp订单优先级中间表（mfgorder_info）同步更新 add by longb 2023-11-21

            SyncTimerStatr(ref m_SendBomDiffToERPTimer, m_SendBomDiffToERPTimer_Elapsed, 20 * 1000);//MES发送BOM差异信息给ERP add zhunan 2025.9.25
        }

        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync;
            timer.Enabled = true;
            timer.Start();
        }

        /// <summary>
        /// 推送给erp工艺信息
        /// add:zhangrj 2023.03.01
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SendToErpWorkflowTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("推送给erp工艺信息启动");
                m_SendToErpWorkflowTimer.Enabled = false;
                //业务类
                SendToErpWorkflowInfo srw = new SendToErpWorkflowInfo();
                srw.RunContainerSync();
                srw.RunRBWorkflowSync();
                //srw.RunRbCommodityTimeSync();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS产品入库军检启动", myError);
            }
            finally
            {
                m_SendToErpWorkflowTimer.Enabled = true;
            }
        }

        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                // LogHelp.WriteLog("Container定时器启动");
                m_SyncTimer.Enabled = false;
                SendDataToERP runMain = new SendDataToERP();
                runMain.RunContainerSync();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }

            m_SyncTimer.Enabled = true;

        }

        /// <summary>
        /// add by longb 2023-11-21
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ErpMfgorderPriorityToMes_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("Erp订单优先级中间表同步更新启动");
                m_ErpMfgorderPriorityToMes.Enabled = false;
                //业务类
                ErpMfgorderPriorityToMes erpp = new ErpMfgorderPriorityToMes();
                erpp.RunErpMfgorderPrioritySync();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("Erp订单优先级中间表同步更新启动", myError);
            }
            finally
            {
                m_ErpMfgorderPriorityToMes.Enabled = true;
            }
        }

        //MES发送BOM差异信息给ERP
        private void m_SendBomDiffToERPTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("MES发送BOM差异信息给ERP服务启动");
                m_SendBomDiffToERPTimer.Enabled = false;
                //业务类
                SendBomDiffToERP erp = new SendBomDiffToERP();
                erp.run();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("MES发送BOM差异信息给ERP服务出错", myError);
            }
            finally
            {
                m_SendBomDiffToERPTimer.Enabled = true;
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }

    }
}
